package com.zhoushihao.class0814;

import java.util.*;

class ScoreRecord {
    private String studentId;
    private String name;
    private String subject;
    private int score;

    public ScoreRecord(String studentId, String name, String subject, int score) {
        this.studentId = studentId;
        this.name = name;
        this.subject = subject;
        this.score = score;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }

    public String getSubject() {
        return subject;
    }

    public int getScore() {
        return score;
    }
}

public class StudentScoreAnalysis2 {
    public static void main(String[] args) {
        String[] scores = {
                "S1001,张三,数学,95",
                "S1002,李四,数学,76",
                "S1003,王五,数学,59",
                "S1001,张三,英语,88",
                "S1002,李四,英语,90",
                "S1003,王五,英语,66",
                "S1001,张三,物理,91",
                "S1002,李四,物理,54",
                "S1003,王五,物理,73",
                "S1004,赵六,数学,88",
                "S1004,赵六,英语,92",
                "S1004,赵六,物理,85"
        };
        List<ScoreRecord> recordList = new ArrayList<>();

        // 1. 构造ScoreRecord集合
        for (int i = 0; i < scores.length; i++) {
            String[] str = scores[i].split(",");
            ScoreRecord record = new ScoreRecord(str[0], str[1], str[2], Integer.parseInt(str[3]));
            recordList.add(record);
        }

        // 2. 列出所有学生姓名
        Set<String> studenNametSet = new HashSet<>();
        for (ScoreRecord record : recordList) {
            studenNametSet.add(record.getName());
        }
        System.out.println("-------------------");
        System.out.println("列出所有学生姓名 : " + studenNametSet);

        // 3. 每个学生总分
        Map<String, List<Integer>> studentScoreMap = new HashMap<>();
        Map<String, Integer> studentTotalScoreMap = new HashMap<>();
        for (ScoreRecord record : recordList) {
            List<Integer> scoreList = studentScoreMap.getOrDefault(record.getName(), new ArrayList<>());
            scoreList.add(record.getScore());
            studentScoreMap.put(record.getName(), scoreList);
        }
        for (Map.Entry<String, List<Integer>> entry : studentScoreMap.entrySet()) {
            List<Integer> scoreList = entry.getValue();
            int totalScore = 0;
            for (Integer score : scoreList) {
                totalScore += score;
            }
            studentTotalScoreMap.put(entry.getKey(), totalScore);
        }
        System.out.println("-------------------");
        for (Map.Entry<String, Integer> entry : studentTotalScoreMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        //4.找出总分最高学生
        int maxScore = Integer.MIN_VALUE;
        String studentMaxScore = null;
        for (Map.Entry<String, Integer> entry : studentTotalScoreMap.entrySet()) {
            if (entry.getValue() > maxScore) {
                maxScore = entry.getValue();
                studentMaxScore = entry.getKey();
            }
        }
        System.out.println("-------------------");
        System.out.println("总分最高学生: " + studentMaxScore + "(" + maxScore + ")");

        // 5. 每个学生平均分
        Map<String, Double> averageScoreMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : studentTotalScoreMap.entrySet()) {
            averageScoreMap.put(entry.getKey(), entry.getValue() * 1.0 / studentScoreMap.get(entry.getKey()).size());
        }
        System.out.println("-------------------");
        for (Map.Entry<String, Double> entry : averageScoreMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + String.format("%.2f",entry.getValue()));
        }

        // 6. 不及格学生记录
        Map<String, Integer> failScoreMap = new HashMap<>();
        for (ScoreRecord record : recordList) {
            if (record.getScore() < 60) {
                failScoreMap.put(record.getName() + "-" + record.getSubject(), record.getScore());
            }
        }
        System.out.println("-------------------");
        System.out.println("不及格记录:");
        for (Map.Entry<String, Integer> entry : failScoreMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        // 7. 每门课程的最高分
        Map<String, Integer> subjectMaxMap = new HashMap<>();
        for (ScoreRecord record : recordList) {if (subjectMaxMap.containsKey(record.getSubject())) {
                if (subjectMaxMap.get(record.getSubject()) < record.getScore()) {
                    subjectMaxMap.put(record.getSubject(), record.getScore());
                }
            } else {
                subjectMaxMap.put(record.getSubject(), record.getScore());
            }
        }
        System.out.println("-------------------");
        System.out.println("每门课程的最高分:");
        for (Map.Entry<String, Integer> entry : subjectMaxMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        // 8. 每门课程平均分
        Map<String, List<Integer>> subjectMap = new HashMap<>();
        Map<String, Double> averageSubjectMap = new HashMap<>();
        for (ScoreRecord record : recordList) {
            List<Integer> scoreList = subjectMap.getOrDefault(record.getSubject(), new ArrayList<>());
            scoreList.add(record.getScore());
            subjectMap.put(record.getSubject(), scoreList);
        }
        for (Map.Entry<String, List<Integer>> entry : subjectMap.entrySet()) {
            int totalScore = 0;
            for (Integer score : entry.getValue()) {
                totalScore += score;
            }
            averageSubjectMap.put(entry.getKey(), totalScore * 1.0 / entry.getValue().size());
        }
        System.out.println("-------------------");
        System.out.println("每门课程平均分:");
        for (Map.Entry<String, Double> entry : averageSubjectMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + String.format("%.2f",entry.getValue()));
        }

        // 9. 平均分最高课程
        double scoreMaxScore = Double.MIN_VALUE;
        String scoreMaxSubject = null;
        for (Map.Entry<String, Double> entry : averageSubjectMap.entrySet()) {
            if (entry.getValue() > scoreMaxScore) {
                scoreMaxScore = entry.getValue();
                scoreMaxSubject = entry.getKey();
            }
        }
        System.out.println("-------------------");
        System.out.println("平均分最高课程: " + scoreMaxSubject);

        // 10. 成绩明细表
        System.out.println("-------------------");
        System.out.println("成绩明细表:");
        Map<String , List<String>> socreInfoMap = new HashMap<>();
        for (ScoreRecord record : recordList) {
            List<String> socreList = socreInfoMap.getOrDefault(record.getName(), new ArrayList<>());
            socreList.add(record.getSubject() + "(" + record.getScore() + ")");
            socreInfoMap.put(record.getName(), socreList);
        }
        for (Map.Entry<String,  List<String>> entry : socreInfoMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue().toString().replace("[","").replace("]",""));
        }
    }
}